package com.dongge;

import com.dongge.entity.ManyToMany1;
import com.dongge.entity.ManyToMany2;
import com.dongge.entity.User;
import com.dongge.service.UserService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhudong on 2016/8/25.
 */
public class Main {
    public static void init(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-db.xml");
        SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        ManyToMany1 many11 = new ManyToMany1();
        many11.setName("dongge");
        ManyToMany1 many12 = new ManyToMany1();
        many12.setName("zeus");
        ManyToMany2 many2 = new ManyToMany2();
        many2.setName("xx-xx-xx");
        HashSet<ManyToMany1> many1Set = new HashSet<ManyToMany1>();
        many1Set.add(many11);
        many1Set.add(many12);
        many2.setMany1(many1Set);
        session.save(many2);
        tx.commit();
        session.close();
    }
    public static void main(String[] args) {
//        init();
//        add();6
//        remove();
        testTx();
    }

    /**
     * 在现有的关系中加入一个元素
     */
    public static void add(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-db.xml");
        SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        ManyToMany2 many2 = (ManyToMany2)session.get(ManyToMany2.class,(long)2);
        ManyToMany1 many1 = new ManyToMany1();
        many1.setName("hello");
//        Set<ManyToMany1> many1Set = new HashSet<ManyToMany1>();
//        many1Set.add(many1);
//        many2.setMany1(many1Set);
        many2.getMany1().add(many1);
        session.saveOrUpdate(many2);
        tx.commit();
        session.close();
    }

    /**
     * 在现有关系中删除一个元素关系（不删除元素本身）
     */
    public static void remove(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-db.xml");
        SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        ManyToMany1 many1 = (ManyToMany1)session.get(ManyToMany1.class,(long)3);
        ManyToMany2 many2 = (ManyToMany2)session.get(ManyToMany2.class,(long)2);
        many2.getMany1().remove(many1);
        tx.commit();
        session.close();
    }

    public static void testTx(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-tx.xml");
        UserService userService = (UserService)applicationContext.getBean("userService");
        User user = new User();
        user.setUsername("dongge");
        user.setPassword("123456");
        userService.save(user);
    }
}
